/* Copyright (C) 2010 Fredrique Samuels, fredriquesamuels@gmail.com 

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "logging.h"
/* Copyright (C) 2010 Fredrique Samuels, fredriquesamuels@gmail.com 

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "logging.h"


namespace tri{



std::fstream Logging::LOG;
int Logging::mLogLevel = tri::INFO;
std::string Logging::mLogFile = "";
bool Logging::mPrintToConsole = true;

void setLoggingFile(std::string file){
	if(Logging::LOG.is_open()){
		Logging::LOG.close();
	}
	Logging::mLogFile = file;
	Logging::LOG.open(file.c_str(),std::ios::out);
}

void setLoggingLevel(int level){
	Logging::mLogLevel = level;
}

void printLogsToConsole(bool b){
	Logging::mPrintToConsole = b;
}

void LOG_INFO(std::string msg){
	if( Logging::mLogLevel == tri::INFO){
		if(Logging::mLogFile==""){
			setLoggingFile("log.txt");
		}
		
		//get time as string
		time_t rawtime;
		struct tm * timeinfo;
		char buffer [100];
		
		std::string lt = "LOG_INFO:: %x %X ";
		lt+=msg;

		time ( &rawtime );
		timeinfo = localtime ( &rawtime );
		strftime (buffer,100,lt.c_str(),timeinfo);
		std::string out(buffer);
		Logging::LOG<<buffer<<std::endl;
		if(Logging::mPrintToConsole){
			std::cout<<buffer<<std::endl;
		}
	}
}

void LOG_SEVERE(std::string msg){
	if( Logging::mLogLevel == tri::INFO){
		if(Logging::mLogFile==""){
			setLoggingFile("log.txt");
		}
		
		//get time as string
		time_t rawtime;
		struct tm * timeinfo;
		char buffer [100];
		
		std::string lt = "LOG_SEVERE:: %x %X ";
		lt+=msg;

		time ( &rawtime );
		timeinfo = localtime ( &rawtime );
		strftime (buffer,100,lt.c_str(),timeinfo);
		std::string out(buffer);
		Logging::LOG<<buffer<<std::endl;
		if(Logging::mPrintToConsole){
			std::cout<<buffer<<std::endl;
		}
	}
}

void LOG_WARNING(std::string msg){
	if( Logging::mLogLevel == tri::INFO){
		if(Logging::mLogFile==""){
			setLoggingFile("log.txt");
		}
		
		//get time as string
		time_t rawtime;
		struct tm * timeinfo;
		char buffer [100];
		
		std::string lt = "LOG_WARNING:: %x %X ";
		lt+=msg;

		time ( &rawtime );
		timeinfo = localtime ( &rawtime );
		strftime (buffer,100,lt.c_str(),timeinfo);
		std::string out(buffer);
		Logging::LOG<<buffer<<std::endl;
		if(Logging::mPrintToConsole){
			std::cout<<buffer<<std::endl;
		}
	}
}

}

